package com.mylearn.myblog.util;


import java.io.Serializable;
import java.util.List;

public class BaseResult implements Serializable {

	public static final String RESULT_OK = "ok";
	public static final String RESULT_NOT_OK = "not_ok";
	public static final String SUCCESS = "成功操作";

	/**
	 * 信息说明
	 */
	private String result;
	/**
	 * 数据
	 */
	private Object data;
	/**
	 * 成功
	 */
	private String success;
	private Cursor cursor;
	private List<Error> errors;

	public static String getResultOk() {
		return RESULT_OK;
	}

	public static String getResultNotOk() {
		return RESULT_NOT_OK;
	}

	public static String getSUCCESS() {
		return SUCCESS;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getSuccess() {
		return success;
	}

	public void setSuccess(String success) {
		this.success = success;
	}

	public Cursor getCursor() {
		return cursor;
	}

	public void setCursor(Cursor cursor) {
		this.cursor = cursor;
	}

	public List<Error> getErrors() {
		return errors;
	}

	public void setErrors(List<Error> errors) {
		this.errors = errors;
	}

	public static BaseResult ok(){
		return createResult(RESULT_OK,null,SUCCESS,null,null);
	}
	public static BaseResult ok(String success){
		return createResult(RESULT_OK,null,success,null,null);
	}
	public static BaseResult ok(Object data){
		return createResult(RESULT_OK,data,SUCCESS,null,null);
	}
	public static BaseResult ok(Object data, Cursor cursor){
		return createResult(RESULT_OK,data,SUCCESS,cursor,null);
	}

	public static BaseResult notOk(List<BaseResult.Error> errors){
		return createResult(RESULT_NOT_OK,null,"",null,errors);
	}
	public static BaseResult notOk(){
		return createResult(RESULT_NOT_OK,null,"",null,null);
	}

	private static BaseResult createResult(String result,Object data,String success,Cursor cursor,List<Error> errors){
		BaseResult baseResult = new BaseResult();
		baseResult.setResult(result);
		baseResult.setData(data);
		baseResult.setSuccess(success);
		baseResult.setCursor(cursor);
		baseResult.setErrors(errors);
		return baseResult;
	}

	public static class Cursor{
		/**
		 * 总条数
		 */
		private int total;
		/**
		 *当前页
		 */
		private int offset;
		/**
		 * 每页条数
		 */
		private int limit;
	}

	public static class Error{
		/**
		 * 字段
		 */
		private String field;
		/**
		 * 错误描述
		 */
		private String message;

		public String getField() {
			return field;
		}

		public void setField(String field) {
			this.field = field;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public Error() {
		}

		public Error(String field, String message) {
			this.field = field;
			this.message = message;
		}

		@Override
		public String toString() {
			return "Error{" +
					"field='" + field + '\'' +
					", message='" + message + '\'' +
					'}';
		}
	}

}
